// 导入登录api
import { login, getInfo, getInfoById } from '@/api/user'
// 导入cookies方法
import { getToken, setToken, removeToken, setTimeStamp } from '@/utils/auth'
import { resetRouter } from '@/router/index'
export default {
  namespaced: true,
  state: {
    // token: localStorage.getItem('token') || ''
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      // localStorage.setItem('token', token)
      setToken(token)
    },
    delToken(state) {
      state.token = ''
      // localStorage.removeItem('token')
      removeToken()
    },
    // 存储用户信息
    setInfo(state, Info) {
      state.userInfo = Info
    },
    // 清除用户信息
    delInfo(state) {
      state.userInfo = ''
    }
  },
  actions: {
    logout(context) {
      context.commit('delToken')
      context.commit('delInfo')
      resetRouter()
    },
    async userLogin(context, payload) {
      const res = await login(payload)
      context.commit('setToken', res.data)
      setTimeStamp()
    },
    async setuserInfo(context) {
      try {
        const res = await getInfo()
        const info = await getInfoById(res.data.userId)
        context.commit('setInfo', { ...res.data, ...info.data })
        return res.data.roles.menus
      } catch (error) {
        console.log(error)
      }
    }
  }
}

